

package com.zf.yichat.controller.packet;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.response.PacketSaleDto;
import com.zf.yichat.mapper.PacketSaleApiMapper;
import com.zf.yichat.mapper.PacketSaleMapper;
import com.zf.yichat.model.PacketSale;
import com.zf.yichat.model.User;
import com.zf.yichat.service.PacketService;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.CheckStatus;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 */
@Controller
@RequestMapping("packet/sale")
public class PacketSaleController extends BaseController {
    @Autowired
    PacketSaleApiMapper packetSaleApiMapper;

    @Autowired
    PacketSaleMapper packetSaleMapper;

    @Autowired
    PacketService packetService;

    @Autowired
    UserService userService;

    @RequestMapping("/index")
    public String index() {
        return "packet/sale-index";
    }

    @RequestMapping("list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Long userId, String name, Integer status) {


        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(packetSaleApiMapper.selectList(userId, name, status, 0), copy -> {
            PacketSaleDto dto = new PacketSaleDto();
            User user = userService.selectById(copy.getUserId());
            dto.setNick(user.getNick());
            dto.setUserId(copy.getUserId());
            dto.setId(copy.getId());
            dto.setName(copy.getName());
            dto.setMoney(copy.getMoney());
            dto.setContent(copy.getContent());
            dto.setImgs(copy.getImgs());
            dto.setNum(copy.getNum());
            dto.setCheckStatus(copy.getCheckStatus());
            dto.setCtime(copy.getCtime());
            dto.setUtime(copy.getUtime());
            dto.setType(copy.getType());
            dto.setExternal(copy.getExternal());
            dto.setSort(copy.getSort());
            dto.setAd(copy.getAd());

            return dto;
        });
    }


    @RequestMapping("ad/save")
    @ResponseBody
    public FsResponse setAd(Integer ad, Long id) {

        PacketSale packetSale = new PacketSale();
        packetSale.setAd(ad);
        packetSale.setId(id);
        packetSaleMapper.updateByPrimaryKeySelective(packetSale);
        return FsResponseGen.success();
    }

    @RequestMapping("external/save")
    @ResponseBody
    public FsResponse setExternal(Integer external, Long id) {

        PacketSale packetSale = new PacketSale();
        packetSale.setExternal(external);
        packetSale.setId(id);
        packetSaleMapper.updateByPrimaryKeySelective(packetSale);
        return FsResponseGen.success();
    }

    @RequestMapping("sort/save")
    @ResponseBody
    public FsResponse setSort(Integer sort, Long id) {

        PacketSale packetSale = new PacketSale();
        packetSale.setSort(sort);
        packetSale.setId(id);
        packetSaleMapper.updateByPrimaryKeySelective(packetSale);
        return FsResponseGen.success();
    }


    @RequestMapping("delete")
    @ResponseBody
    public FsResponse delete(Long id) {

        PacketSale packetSale = new PacketSale();
        packetSale.setStatus(1);
        packetSale.setId(id);
        packetSaleMapper.updateByPrimaryKeySelective(packetSale);
        return FsResponseGen.success();
    }

    @RequestMapping("check")
    @ResponseBody
    public FsResponse check(Long id, Integer status, String reason) {

        PacketSale packetSale = packetSaleMapper.selectByPrimaryKey(id);
        Contracts.assertNotNull(packetSale, "值不存在");
        //通过
        if (status == 1) {
            //暂停->启动
            if (packetSale.getCheckStatus() == CheckStatus.PAUSE.getVal()) {
                return packetService.toggleSaleAdmin(id);
            } else if (packetSale.getCheckStatus() == CheckStatus.FAILED.getVal()) {
                //拒绝->重审
                return packetService.createSaleAgain(packetSale);
            } else {
                //审核通过
                return packetService.createSaleSuccess(packetSale);
            }
        } else if (status == 2) {
            //拒绝
            packetSale.setRefuseReason(reason);
            return packetService.createSaleFail(packetSale);
        } else if (status == 3) {
            //终止
            return packetService.terminateSaleByAdmin(id);
        } else if (status == 4) {
            //暂停
            return packetService.toggleSaleAdmin(id);
        }
        return FsResponseGen.fail();

    }

    @RequestMapping("/detail/index")
    public String detail(Long id) {
        getRequest().setAttribute("packetSale", packetSaleMapper.selectByPrimaryKey(id));
        return "packet/sale-detail";
    }

}
